<?php

namespace Index\Controller;

use Think\Controller;
use Index\ViewModel;
use Index\Service;

/**
 * 说明：统计管理控制器
 * 作者：王楠
 * 创建日期：2017-09-15
 */
class StatisticsController extends CommonController {

    public function statisticslist() {
	$type = I('type');
	$userinfoModel = M('chat_userinfo'); //用户邀请表
	$usercalllogModel = M('chat_usercalllog'); //用户通话日志表
	$balancelogModel = M('chat_balancelog'); //用户余额日志表
	$app_code = $this->app_code;
	if (empty($app_code)) {
	    $app_code = I('app_code');
	}
	$login_type = $this->login_type;
	$admin_uid = $this->admin_uid;
	$map = array('_complex' => '1=1');
	$map['app_flag'] = $app_code;
	if ($login_type == '2') {
	    $map['agent_uid'] = $admin_uid;
	}
	$data['user_sum'] = $userinfoModel->where($map)->count();   //用户增长量
	$data['user_sum'] = empty($data['user_sum']) ? '0' : (int) $data['user_sum'];
	$data['call_sum'] = $usercalllogModel->where($map)->where(array('state' => '1', 'type' => '1'))->count();  //通话增长量
	$data['call_sum'] = empty($data['call_sum']) ? '0' : (int) $data['call_sum'];
//	$data['money_out_sum'] = $balancelogModel->where($map)->where(array('state' => '1', 'type' => '1'))->sum('money');  //消费金额（金币）
//	$data['money_in_sum'] = $balancelogModel->where($map)->where(array('state' => '1', 'type' => '3'))->sum('money');  //收益金额（金币）
//	$data['money_recharge_sum'] = $balancelogModel->where($map)->where(array('state' => '1', 'type' => '2'))->sum('money');  //充值金额（金币）
//	$data['money_withdraw_sum'] = $balancelogModel->where($map)->where(array('state' => '1', 'type' => '4'))->sum('money');  //提现金额（金币）
//	$data['money_other_sum'] = $balancelogModel->where($map)->where(array('state' => '1', 'type' => '5'))->sum('money');  //其他金额（管理员更改等）（金币）
	$field = 'type, sum(money) as total';
	$group = 'type';
	$balancelog_sum = $balancelogModel->where($map)->where(array('state' => '1'))->field($field)->group($group)->select();
	foreach ($balancelog_sum as $key => $value) {
	    switch ($value['type']) {
		case '1':
		    $data['money_out_sum'] = $value['total'];
		    break;
		case '2':
		    $data['money_recharge_sum'] = $value['total'];
		    break;
		case '3':
		    $data['money_in_sum'] = $value['total'];
		    break;
		case '4':
		    $data['money_withdraw_sum'] = $value['total'];
		    break;
		case '5':
		    $data['money_other_sum'] = $value['total'];
		    break;
		default:
		    break;
	    }
	}
	$data['money_out_sum'] = empty($data['money_out_sum']) ? '0' : (int) $data['money_out_sum'];
	$data['money_in_sum'] = empty($data['money_in_sum']) ? '0' : (int) $data['money_in_sum'];
	$data['money_recharge_sum'] = empty($data['money_recharge_sum']) ? '0' : (int) $data['money_recharge_sum'];
	$data['money_withdraw_sum'] = empty($data['money_withdraw_sum']) ? '0' : (int) $data['money_withdraw_sum'];
	$data['money_other_sum'] = empty($data['money_other_sum']) ? '0' : (int) $data['money_other_sum'];
	$this->assign(array(
	    'type' => $type,
	    'data' => $data,
	    'app_code' => $app_code,
	));
	$this->display();
    }
    
    public function statisticstotal(){
	$type = I('type');
	$state_enum = getenumbykey('app_state');
	$state_arr = getarrbyenum($state_enum);
	$state = '1';
	$begintime = trim(I('txtbegintime'));
	$endtime = trim(I('txtendtime'));
	$keyword = trim(I('txtkeyword'));
	switch ($type) {
	    case '1':
		$columnname = '日增长统计';
		break;
	    case '2':
		$columnname = '周增长统计';
		break;
	    case '3':
		$columnname = '月增长统计';
		break;
	    case '4':
		$columnname = '年增长统计';
		break;
	    default:
		break;
	}
	if (!empty($state)) {
	    $map['app_state'] = $state;
	}
	if (!empty($begintime) || !empty($endtime)) {
	    if (!empty($begintime) && !empty($endtime)) {
		$map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
	    } else if (!empty($begintime)) {
		$map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
	    } else if (!empty($endtime)) {
		$map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
	    }
	}
	if (!empty($keyword)) {
	    $where['id'] = array('eq', $keyword);
	    $where['app_flag'] = array('like', '%' . $keyword . '%');
	    $where['app_name'] = array('like', '%' . $keyword . '%');
	    $where['_logic'] = 'or';
	    $map['_complex'] = $where;
	}
	$model = M('chat_systemset');
	$total = $model->where($map)->count();
	$row = '10';
	$page = new Service\PageService($total, $row);
	$fpage = $page->fpage();
	$field = array(
	    'id', 'app_flag', 'app_logo', 'app_name', 'app_state', 'ctime'
	);
	$order = 'ctime desc, id desc';
	$data = $model->where($map)->limit($page->listfirst, $page->listRows)->field($field)->order($order)->select();
	foreach ($data as $key => $value) {
	    $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
	}
	$search = array('statesel' => $state, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
	$this->assign(array(
	    'type' => $type,
	    'data' => $data,
	    'fpage' => $fpage,
	    'total' => $total,
	    'search' => $search,
	    'columnname' => $columnname,
	    'statearr' => $state_arr,
	));
	$this->display();
    }

    /**
     * 收支（消费/充值/赚取/提现）明细表
     * @return type
     */
    public function getmessagedata() {
	if (!IS_POST) {
	    $this->error('非法访问！');
	}
	$app_code = $this->app_code;
	if (empty($app_code)) {
	    $app_code = I('app_code');
	}
	$login_type = $this->login_type;
	$admin_uid = $this->admin_uid;
	$page_size =  I('pagesize', '5');
	$page = I('page', '0');
	$page_index = ($page - 1) * $page_size;
	$timetype = I('timetype'); //时间类型：日，星期，月，年
	$type = I('type'); //统计类型：1消费，3收益，2充值，4提现，5其他（管理员更改）
	$map = array('_complex' => '1=1');
	$map['app_flag'] = $app_code;
	$map['state'] = '1';
	if ($login_type == '2') {
	    $map['agent_uid'] = $admin_uid;
	}
	if (!empty($type)) {
	    $map['type'] = $type;
	}
	switch ($type) {
	    case '1':
		$togglestr = '-';
		break;
	    case '2':
		$togglestr = '+';
		break;
	    case '3':
		$togglestr = '+';
		break;
	    case '4':
		$togglestr = '-';
		break;
	    case '5':
		$togglestr = '';
		break;
	    default:
		break;
	}
	switch ($timetype) {
	    case '1':
		$field = 'ctime, sum(money) as total';
		$group = 'DATE_FORMAT(FROM_UNIXTIME(ctime),\'%Y%m%d\')';
		$date_format = 'Y-m-d';
		break;
	    case '2':
		$field = 'ctime, sum(money) as total';
		$group = 'DATE_FORMAT(FROM_UNIXTIME(ctime),\'%Y_%u\')';
		$date_format = 'Y-m-d';
		break;
	    case '3':
		$field = 'ctime, sum(money) as total';
		$group = 'DATE_FORMAT(FROM_UNIXTIME(ctime),\'%Y%m\')';
		$date_format = 'Y-m';
		break;
	    case '4':
		$field = 'ctime, sum(money) as total';
		$group = 'DATE_FORMAT(FROM_UNIXTIME(ctime),\'%Y\')';
		$date_format = 'Y';
		break;
	    default:
		break;
	}
	$model = M('chat_balancelog'); //账户余额日志表
	$total = $model->where($map)->field('count(*)')->group($group)->select();
	$total = count($total);
	$data = $model->where($map)->field($field)->group($group)->limit($page_index, $page_size)->order('ctime desc')->select();
	foreach ($data as $key => $value) {
	    $data[$key]['date'] = date($date_format, $data[$key]['ctime']);
	    $data[$key]['total'] = (int) $data[$key]['total'];
//	    $data[$key]['total'] = $togglestr . (int) $data[$key]['total'];
	}
//	$response = $data;
	$response = array('ret' => '1001', 'total' => $total, 'data' => $data);
	return $this->ajaxReturn($response, 'json');
    }

    /**
     * 用户增长量明细表
     * @return type
     */
    public function getmessagedata5() {
	if (!IS_POST) {
	    $this->error('非法访问！');
	}
	$app_code = $this->app_code;
	if (empty($app_code)) {
	    $app_code = I('app_code');
	}
	$login_type = $this->login_type;
	$admin_uid = $this->admin_uid;
	$page_size =  I('pagesize', '5');
	$page = I('page', '0');
	$page_index = ($page - 1) * $page_size;
	$timetype = I('timetype'); //时间类型：日，星期，月，年
	$map = array('_complex' => '1=1');
	$map['app_flag'] = $app_code;
	if ($login_type == '2') {
	    $map['agent_uid'] = $admin_uid;
	}
	switch ($timetype) {
	    case '1':
		$field = 'ctime, count(*) as total';
		$group = 'DATE_FORMAT(FROM_UNIXTIME(ctime),\'%Y%m%d\')';
		$date_format = 'Y-m-d';
		break;
	    case '2':
		$field = 'ctime, count(*) as total';
		$group = 'DATE_FORMAT(FROM_UNIXTIME(ctime),\'%Y_%u\')';
		$date_format = 'Y-m-d';
		break;
	    case '3':
		$field = 'ctime, count(*) as total';
		$group = 'DATE_FORMAT(FROM_UNIXTIME(ctime),\'%Y%m\')';
		$date_format = 'Y-m';
		break;
	    case '4':
		$field = 'ctime, count(*) as total';
		$group = 'DATE_FORMAT(FROM_UNIXTIME(ctime),\'%Y\')';
		$date_format = 'Y';
		break;
	    default:
		break;
	}
	$model = M('chat_userinfo'); //用户邀请表
	$total = $model->where($map)->field('count(*)')->group($group)->select();
	$total = count($total);
	$data = $model->where($map)->field($field)->group($group)->limit($page_index, $page_size)->order('ctime desc')->select();
	foreach ($data as $key => $value) {
	    $data[$key]['date'] = date($date_format, $data[$key]['ctime']);
	    $data[$key]['total'] = (int) $data[$key]['total'];
//	    $data[$key]['total'] = '+' . (int) $data[$key]['total'];
	}
//	$response = $data;
	$response = array('ret' => '1001', 'total' => $total, 'data' => $data);
	return $this->ajaxReturn($response, 'json');
    }

    /**
     * 通话增长量明细表
     * @return type
     */
    public function getmessagedata6() {
	if (!IS_POST) {
	    $this->error('非法访问！');
	}
	$app_code = $this->app_code;
	if (empty($app_code)) {
	    $app_code = I('app_code');
	}
	$login_type = $this->login_type;
	$admin_uid = $this->admin_uid;
	$page_size =  I('pagesize', '5');
	$page = I('page', '0');
	$page_index = ($page - 1) * $page_size;
	$timetype = I('timetype'); //时间类型：日，星期，月，年
	$map = array('_complex' => '1=1');
	$map['app_flag'] = $app_code;
	$map['state'] = '1';
	$map['type'] = '1';
	if ($login_type == '2') {
	    $map['agent_uid'] = $admin_uid;
	}
	switch ($timetype) {
	    case '1':
		$field = 'ctime, count(*) as total';
		$group = 'DATE_FORMAT(FROM_UNIXTIME(ctime),\'%Y%m%d\')';
		$date_format = 'Y-m-d';
		break;
	    case '2':
		$field = 'ctime, count(*) as total';
		$group = 'DATE_FORMAT(FROM_UNIXTIME(ctime),\'%Y_%u\')';
		$date_format = 'Y-m-d';
		break;
	    case '3':
		$field = 'ctime, count(*) as total';
		$group = 'DATE_FORMAT(FROM_UNIXTIME(ctime),\'%Y%m\')';
		$date_format = 'Y-m';
		break;
	    case '4':
		$field = 'ctime, count(*) as total';
		$group = 'DATE_FORMAT(FROM_UNIXTIME(ctime),\'%Y\')';
		$date_format = 'Y';
		break;
	    default:
		break;
	}
	$model = M('chat_usercalllog'); //用户通话日志表
	$total = $model->where($map)->field('count(*)')->group($group)->select();
	$total = count($total);
	$data = $model->where($map)->field($field)->group($group)->limit($page_index, $page_size)->order('ctime desc')->select();
	foreach ($data as $key => $value) {
	    $data[$key]['date'] = date($date_format, $data[$key]['ctime']);
	    $data[$key]['total'] = (int) $data[$key]['total'];
//	    $data[$key]['total'] = '+' . (int) $data[$key]['total'];
	}
//	$response = $data;
	$response = array('ret' => '1001', 'total' => $total, 'data' => $data);
	return $this->ajaxReturn($response, 'json');
    }

}
